//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package project_dao

import (
	"context"

	"atomgit.com/openlinksaas-org/api-server/dao/db_ns"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/project_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type TagInfoDbItem struct {
	TagId             string `bson:"_id"`
	ProjectId         string `bson:"projectId"`
	TagName           string `bson:"tagName"`
	CreateTime        int64  `bson:"createTime"`
	BgColor           string `bson:"bgColor"`
	UseInTask         bool   `bson:"useInTask"`
	UseInBug          bool   `bson:"useInBug"`
	UseInReq          bool   `bson:"useInReq"`
	UseInSpritSummary bool   `bson:"useInSpritSummary"`
	UseInEntry        bool   `bson:"useInEntry"`
}

func (item *TagInfoDbItem) ToTagInfo() *project_api.TagInfo {
	return &project_api.TagInfo{
		TagId:             item.TagId,
		TagName:           item.TagName,
		CreateTime:        item.CreateTime,
		BgColor:           item.BgColor,
		UseInTask:         item.UseInTask,
		UseInBug:          item.UseInBug,
		UseInReq:          item.UseInReq,
		UseInSpritSummary: item.UseInSpritSummary,
		UseInEntry:        item.UseInEntry,
	}
}

type _TagInfoDao struct {
	mc *mongo.Collection
}

func newTagInfoDao(db *mongo.Database) (*_TagInfoDao, error) {
	mc := db.Collection(db_ns.DB_NS_PROJECT + "tag")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"projectId", 1}, {"createTime", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_TagInfoDao{mc: mc}, nil
}

func (db *_TagInfoDao) Insert(ctx context.Context, dbItem *TagInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_TagInfoDao) Update(ctx context.Context, dbItem *TagInfoDbItem) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": dbItem.TagId, "projectId": dbItem.ProjectId}, bson.M{"$set": dbItem})
	return err
}

func (db *_TagInfoDao) Remove(ctx context.Context, projectId, tagId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": tagId, "projectId": projectId})
	return err
}

func (db *_TagInfoDao) Exist(ctx context.Context, projectId, tagId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": tagId, "projectId": projectId})
	return count > 0, err
}

func (db *_TagInfoDao) Get(ctx context.Context, projectId, tagId string) (*TagInfoDbItem, error) {
	retItem := &TagInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": tagId, "projectId": projectId}).Decode(retItem)
	return retItem, err
}

func (db *_TagInfoDao) List(ctx context.Context, projectId string, tagScopeType project_api.TAG_SCOPE_TYPE) ([]*TagInfoDbItem, error) {
	query := bson.M{"projectId": projectId}
	if tagScopeType == project_api.TAG_SCOPE_TYPE_TAG_SCOPE_TASK {
		query["useInTask"] = true
	} else if tagScopeType == project_api.TAG_SCOPE_TYPE_TAG_SCOPE_BUG {
		query["useInBug"] = true
	} else if tagScopeType == project_api.TAG_SCOPE_TYPE_TAG_SCOPE_REQ {
		query["useInReq"] = true
	} else if tagScopeType == project_api.TAG_SCOPE_TYPE_TAG_SCOPE_IDEA {
		query["useInIdea"] = true
	} else if tagScopeType == project_api.TAG_SCOPE_TYPE_TAG_SCOPE_SPRIT_SUMMARY {
		query["useInSpritSummary"] = true
	} else if tagScopeType == project_api.TAG_SCOPE_TYPE_TAG_SCOPE_ENTRY {
		query["useInEntry"] = true
	}
	cursor, err := db.mc.Find(ctx, query, options.Find().SetSort(bson.M{"createTime": 1}))
	if err != nil {
		return nil, err
	}
	retList := []*TagInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}
